package org.hashdb.client.console.completer;

import jline.console.completer.Completer;

import java.util.List;

/**
 * Date: 2024/1/12 1:01
 *
 * @author huanyuMake-pecdle
 * @version 0.0.1
 */
public class OptionsCompleter implements Completer, RemoteCompleter {
    @Override
    public int complete(String str, int cursor, List<CharSequence> candidates) {
        if (!str.startsWith("-") || str.length() == 1) {
            return -1;
        }
        if (str.charAt(1) == '-') {
            // 全称
            var prefix = str.substring(2);
            boolean match = false;
            for (Option option : Option.values()) {
                if (option.fullName.startsWith(prefix)) {
                    candidates.add(option.fullName);
                    match = true;
                }
            }
            if (match) {
                return cursor - prefix.length();
            } else {
                return -1;
            }
        }
        // 简称
        var prefix = str.substring(1);
        boolean match = false;
        for (Option OptionCompleter : Option.values()) {
            for (String shortName : OptionCompleter.shortName) {
                if (!shortName.startsWith(prefix)) {
                    continue;
                }
                candidates.add(shortName);
                match = true;
                break;
            }
        }
        if (match) {
            return cursor - prefix.length();
        } else {
            return -1;
        }
    }

}
